package com.lynn.salary.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
public class ToolMethod {
	
	public static String replaceBlank(String s) {
		return s.replaceAll("\\s+", "");
	}
	
	public static String escSpecChar(String q) {
		return q.replace("%", "\\%");
	}
	
	public static void print(String k,String v) {
		
		System.out.print(k+":");
		int s = 0;
		int e = 0;
		int p = 80;
		for(;;) {
			s = e;
			e+=p;
			if(e > v.length()) {
				if(s == 0) {
					System.out.print("\t");
				} else {
					System.out.print("\t\t");
				}
				System.out.println(v.substring(s,v.length()));
				break;
			}
			if(s == 0) {
				System.out.print("\t");
			} else {
				System.out.print("\t\t");
			}
			System.out.println(v.substring(s,e));
		}
		
	}
	
	public static String getDate(String format) {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat f = new SimpleDateFormat(format);
		
		return f.format(c.getTime());
	}
	
	public static String formatDate(Date d,String format) {
		SimpleDateFormat f = new SimpleDateFormat(format);
		return f.format(d);
	}
	
	public static boolean copy(File src, File dst) {
		int BUFFER_SIZE = 2 * 1024;
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src),
						BUFFER_SIZE);
				out = new BufferedOutputStream(new FileOutputStream(dst),
						BUFFER_SIZE);
				byte[] buffer = new byte[BUFFER_SIZE];
				while (in.read(buffer) > 0) {
					out.write(buffer);
				}
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	
	public static String autoId() {
		Calendar c = Calendar.getInstance();
		StringBuffer id = new StringBuffer();
		id.append(getDate("yyyyMMddHHmmss"));
		id.append("$");
		id.append(getCharAndNumr(4));
		return id.toString();
	}

	private static String change(String s) {
		if (s.length() == 1)
			return 0 + s;
		else
			return s;
	}

	public static void main(String[] args) {
//		System.out.println(getDate("yyyyMM"));
//		System.out.println(getDate("yyyyMMdd"));
//		System.out.println(autoId());
		String ids [] =new String[]{"1","3","4","5"};
		System.out.println(join(ids, ",", "'"));
		System.out.println(replaceBlank("adf   	 as  "));
	}

	public static String getCharAndNumr(int length) {
		String val = "";

		Random random = new Random();
		for (int i = 0; i < length; i++) {
			String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; // 输出字母还是数字

			if ("char".equalsIgnoreCase(charOrNum)) // 字符串
			{
				int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; // 取得大写字母还是小写字母
				val += (char) (choice + random.nextInt(26));
			} else if ("num".equalsIgnoreCase(charOrNum)) // 数字
			{
				val += String.valueOf(random.nextInt(10));
			}
		}

		return val;
	}

	public static String change2SetMethodName(String field) {
		char f = field.charAt(0);
		field = "set" + (f + "").toUpperCase() + field.substring(1);
		return field;
	}

	public static String change2GetMethodName(String field) {
		char f = field.charAt(0);
		field = "get" + (f + "").toUpperCase() + field.substring(1);
		return field;
	}

	public static String getTableName(Class<?> c) {
		Method getTableName;
		try {
			getTableName = c.getMethod("getTableName");
			String tn = (String)getTableName.invoke(c);
			return tn;
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String toPinyin(String hanzi) {
		StringBuffer pinyin = new StringBuffer();
		 HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
		 outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
	     outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

		for(int i=0;i<hanzi.length();i++) {
			char r = hanzi.charAt(i);
			try {
				pinyin.append(PinyinHelper.toHanyuPinyinStringArray(r, outputFormat)[0]);
			} catch (Exception e) {
			}
		}
		System.out.println(pinyin);
		return pinyin.toString();
	}
	
	public static String findFromMap(List<Map<String,Object>> src , String kq,String dep) {
		for(Map<String,Object> item : src) {
			if(item.get("aname").equals(kq)) {
				if(item.get("dname").equals(dep)) {
					return ""+item.get("tv");
				}
			}
		}
		return "0";
	}
	
	public static String join(String[]ids,String sp,String sur) {
		StringBuffer sql = new StringBuffer();
		int flag = 0;
		for(String id:ids) {
			if(flag++ != 0) {
				sql.append(sp);
			}
			sql.append(sur);
			sql.append(id);
			sql.append(sur);
		}
		
		return sql.toString();
	}
	
	public static Map<String,String> getPaymentValue(Double salary) {
		Double yanglaoR = 0.08;
		Double yiliaoR = 0.02;
		Double shiyeR = 0.01;
		Double gongjjR = 0.07;
		Double yanglao;
		Double yiliao;
		Double shiye;
		Double gongjj;
		Double payment;
		Double sygz; //扣除五险一金剩余工资
		Double ynssr;//应纳税收入
		Double grsds=0.00;//应缴纳的个人所得税
	    Double gz;
	   
		List<Double> values = new ArrayList<Double>();
		yanglao = salary * yanglaoR;
		yiliao = salary * yiliaoR;
		shiye = salary * shiyeR;
		gongjj = salary * gongjjR;
		payment = yanglao + yiliao + shiye + gongjj;
		sygz=salary-payment;
		ynssr=salary-payment-3500.00;
		
		 if(ynssr<=1500){
		grsds=ynssr*0.03-0.00;
		}
		else if(1500<ynssr&&ynssr<=4500){
		grsds=ynssr*0.1-105;
		}
		else if(4500<ynssr&&ynssr<=9000){
		grsds=ynssr*0.2-555;	
		}
		else if(9000<ynssr&&ynssr<=35000){
		grsds=ynssr*0.25-1005;	
		}
		else if(5000<ynssr&&ynssr<=55000){
			grsds=ynssr*0.30-2755;	
		}	
		else if(80000<ynssr&&ynssr<=80000){
			grsds=ynssr*0.35-5505;	
		}		
		else if(ynssr>=80000){
			grsds=ynssr*0.45-13505;	
		}	
		if(grsds<=0.00){
			grsds=0.0;
		}
		gz=salary-payment-grsds;
		 BigDecimal  yanglao1  =   new   BigDecimal(yanglao); 
		 double    yanglaozhh =    yanglao1.setScale(1,   BigDecimal.ROUND_HALF_UP).doubleValue();
		 BigDecimal  yiliao1  =   new   BigDecimal(yiliao); 
		 double    yiliaozhh =    yiliao1.setScale(1,   BigDecimal.ROUND_HALF_UP).doubleValue();
		 BigDecimal  shiye1  =   new   BigDecimal(shiye); 
		 double    shiyezhh =    shiye1.setScale(1,   BigDecimal.ROUND_HALF_UP).doubleValue();
		 BigDecimal  gongjj1  =   new   BigDecimal(gongjj); 
		 double    gongjjzhh =    gongjj1.setScale(1,   BigDecimal.ROUND_HALF_UP).doubleValue();
		 BigDecimal payment1  =   new   BigDecimal(payment); 
		 double    paymentzhh =    payment1.setScale(1,   BigDecimal.ROUND_HALF_UP).doubleValue();
		 BigDecimal grsds1  =   new   BigDecimal(grsds); 
		 double    grsdszhh =    grsds1.setScale(1,   BigDecimal.ROUND_HALF_UP).doubleValue();
		values.add(0, yanglaozhh);
		values.add(1, yiliaozhh);
		values.add(2, shiyezhh);
		values.add(3,  gongjjzhh);
		values.add(4, paymentzhh);
		values.add(5, grsdszhh);
		values.add(6, gz);
		
		Map<String,String> map = new HashMap<String, String>();
		map.put("养老保险", ""+yanglaozhh);
		map.put("医疗保险", ""+yiliaozhh);
		map.put("失业保险", ""+shiyezhh);
		map.put("个税", ""+grsdszhh);
		map.put("住房公积金", ""+gongjjzhh);
		return map;
	}

}
